$(function () {
    'use strict';
    //首页
    $(document).on("pageInit", "#page-index", function (e, id, page) {
        if(key){
            $("#search").val(key);
        }
        $(".infinite-scroll-preloader").hide();
        $("#searchBtn").click(function () {
            searchData();
        });
        $("#leftSide a[name='menu']").click(function () {
            var href = this.getAttribute('data-href');
            if(href){
                $.router.load(href);
            }
        });
        $("#sort .tab-link").click(function () {
            if($(this).attr("name") == "checkbox"){
                if($(this).find("input").is(':checked')){
                    $(this).find("input").removeAttr("checked");
                } else{
                    $(this).find("input").attr("checked","true");
                }
            }else {
                if($(this).attr("sortname")=="price_" && $(this).hasClass("active")){
                    $(this).find("span[name='icon']").removeAttr("class");
                    if($(this).attr("sortmethod")=="asc"){
                        $(this).attr("sortmethod","des");
                        $(this).find("span[name='icon']").attr("class","icon icon-down");
                    } else {
                        $(this).attr("sortmethod","asc")
                        $(this).find("span[name='icon']").attr("class","icon icon-up");
                    }
                }else {
                    $(this).siblings().each(function (index,item) {
                        $(item).removeClass("active");
                    });
                    $(this).addClass("active");
                }
            }
            searchData();
        });
    });
    //商品详情
    $(document).on("pageInit", "#page-details", function (e, id, page) {
        $.showPreloader();
        getInfo();
        $.reinitSwiper("#page-details");
        $("#tpwd").click(function () {
            getTpwd("#tpwd");
        });
        $(".share-btn").click(function () {
            $.popup('.popup-about');
        });
        $("#copyShare").click(function () {
            copyShare();
        });
    });
    //淘口令转换
    $(document).on("pageInit", "#page-convert", function (e, id, page) {

    });
    $.init();
});

function getInfo() {

    var url = baseUrl+"q/info";
    var data = {
        itemId:getUrlParam("itemId"),
        couponId:getUrlParam("couponId")
    };
    $.ajax({
        url:url,
        type:"post",
        data:data,
        async:false,
        success:function(result){

            if(result.code == 50000){
                var info = result.data;
                //图片幻灯片
                var imageSwiper = '<div class="swiper-slide"><img src="'+info.img+'" alt="" style="max-height: 100%;max-width: 100%;margin: auto"></div>';
                var smallImages = info.smallImages;
                if (isNotEmpty(smallImages)){
                    $.each(smallImages,function (index,smallImage) {
                        imageSwiper += '<div class="swiper-slide"><img src="'+smallImage+'" alt="" style="max-height: 100%;max-width: 100%;margin: auto"></div>';
                    });
                }
                $("#imageSwiper").html(imageSwiper);
                //卡头商品标题
                $("#title").html(info.title);
                //推广链接URl
                $("#itemUrl").attr("href",info.itemUrl);

                if(info.hasCoupon){
                    //有券
                    $("#cv").html("￥"+info.zkPrice);
                    //淘宝价
                    $("#pv").html("￥"+info.price);
                    //券
                    $("#couponAmount").html("券:"+info.couponAmount+"元");
                    //优惠券URl
                    $("#couponUrl").attr("href",info.couponUrl);
                }else {
                    //没券
                    $("#ck").hide();
                    $("#cv").hide();
                    $("#pk").css("font-size",".85rem").css("font-weight","bold")
                    $("#pv").css("font-size",".95rem").css("color","red").css("font-weight","bold");
                    //淘宝价
                    $("#pv").html("￥"+info.price);
                    //优惠券URl改为推广链接
                    $("#couponUrl").attr("href",info.itemUrl);
                }

                //淘口令
                if(isNotEmpty(info.tpwd)){
                    //有淘口令
                    $("#tpwd").attr("data-clipboard-text",info.tpwd);
                    $("#tool1").show();
                }else {
                    //没有淘口令
                    $("#tool2").show();
                }

                //月销量
                $("#volume").html(info.volume);
                //地址
                $("#address").html(info.address);
                //店
                var shopHtml = '';
                switch (info.shopType) {
                    case 0: //淘宝
                        shopHtml+='<img style="margin-right: 6px;" src="'+tbIco+'"/>';
                        break;
                    case 1: //天猫
                        shopHtml += '<img style="margin-right: 6px;" src="'+tmIco+'"/>';
                        break;
                    default:
                        shopHtml += '';
                        break;
                }
                $("#shop").html("<span>"+shopHtml+info.shopTitle+"</span>");
                getShare(info)
            }else {
                $.alert(result.message,'数据请求失败...');
            }
            $.hidePreloader();
        },
        error:function(result){
            $.hidePreloader();
            $.alert('请联系管理员处理','网站崩溃了...');
        }
    });
}

function getTpwd(btn) {
    var copyTpwd = new ClipboardJS(btn);
    copyTpwd.on('success', function(e) {
        //$.toast("获取成功!打开手机淘宝查看...",2000,"bottom");
        $.alert("获取成功，打开手机淘宝查看！");
    });
    copyTpwd.on('error', function(e) {
        var tpwd = $("#tpwd").attr("data-clipboard-text");
        $.alert(tpwd, '复制失败，请手动复制！');
    });
}

function getShare(info) {
    var text = "";
    text += info.title+"\n";
    text += "【淘宝价】"+info.price+"元\n";
    if(isNotEmpty(info.zkPrice)){
        text += "【券后价】"+info.zkPrice+"元\n";
    }
    if(isNotEmpty(info.shortUrl)){
        text += "【下单链接】"+info.shortUrl+"\n";
    }
    text += "-------------------------\n";
    if(isNotEmpty(info.tpwd)) {
        text += "复制这条消息，" + info.tpwd + "，打开【手机淘宝】即可查看\n";
    }
    text += "-------------------------\n";
    text += "本站地址：https://q.hayye.cn，需要领券搜一搜！";
    $("#shareContent").val(text)
}

function copyShare() {
    var copy = new ClipboardJS("#copyShare");
    copy.on('success', function(e) {
        $.toast("获取成功！");
    });
    copy.on('error', function(e) {
        $.alert('复制失败，请手动复制！');
    });
}